package com.jiepos.mpos.biz.http.service;

import java.io.File;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jiepos.mpos.biz.appInfM.service.CustomMsgService;
import com.jiepos.mpos.biz.appInfM.service.HeadAdvService;
import com.jiepos.mpos.biz.appInfM.service.HelpService;
import com.jiepos.mpos.biz.appInfM.service.LogErrorReportService;
import com.jiepos.mpos.biz.appInfM.service.MessageInfService;
import com.jiepos.mpos.biz.appInfM.service.OrganizationService;
import com.jiepos.mpos.biz.appInfM.service.ShareService;
import com.jiepos.mpos.biz.code.service.AppAreaCdService;
import com.jiepos.mpos.biz.code.service.InfCnapsService;
import com.jiepos.mpos.biz.code.service.SuperBankService;
import com.jiepos.mpos.biz.customer.service.AppCustomerInfoService;
import com.jiepos.mpos.biz.operationlog.service.SmsCodeFlowService;
import com.jiepos.mpos.biz.user.service.LoginService;
import com.jiepos.mpos.common.constant.ExceptionConstant;
import com.jiepos.mpos.common.constant.RspCodeConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppLogErrorReportDao;
import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.common.entity.App_Log_ErrorReportEntity;
import com.jiepos.mpos.common.entity.App_Message_InfEntity;
import com.jiepos.mpos.common.entity.GpsEntity;
import com.jiepos.mpos.common.service.AbrBaseHttpService;
import com.jiepos.mpos.common.service.SystemService;
import com.jiepos.mpos.core.entity.MposResponse;
import com.jiepos.mpos.core.util.CollectionUtil;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.FileUtils;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;
import com.jiepos.mpos.inter.message.service.RandService;

/**
 * <p>标题: —— app客户端一些不涉及到业务的服务处理类</p>
 * <p>模块: app通信模块</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 *
 * @author 娄伟峰
 * @version 1.0
 * @history（历次修订内容、修订人、修订时间等）
 * @since 2017年9月15日 下午1:46:53
 */
@Service
public class HttpCommonService extends AbrBaseHttpService {

    public static DefaultHttpClient httpclient;

    static {
        httpclient = new DefaultHttpClient();
    }

    private final static Log log = LogFactory.getLog(HttpCommonService.class);
    @Autowired
    private HeadAdvService headAdvService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private SmsCodeFlowService smsCodeFlowService;
    @Autowired
    private AppCustomerInfoService appCustomerInfoService;
    @Autowired
    private AppAreaCdService appAreaCdService;
    @Autowired
    private SuperBankService superBankService;
    @Autowired
    private InfCnapsService infCnapsService;
    @Autowired
    private MessageInfService messageInfService;
    @Autowired
    private CustomMsgService customMsgService;
    @Autowired
    private HelpService helpService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private RandService randService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private ShareService shareService;
    @Autowired
    private LogErrorReportService logErrorReportService;
    @Autowired
    private AppLogErrorReportDao appLogErrorReportDao;
    /**
     * 客户端获取验证码
     *
     * @param custom_uuid
     * @param phone
     * @return
     */
    public Map getHttpRand(HttpServletRequest request, String serial_number, String sign, String random_number, String version,
                           String phone, String custom_uuid, String custom_name, String certificate_no, String type, GpsEntity gpsEntity) {

        String serviceData = StringUtils.objToString(serial_number) + StringUtils.objToString(phone);
        Map rspMap = this.checkCommonSign(request, serviceData, sign, "获取验证码");
        subLog.info("短信验证码接口调用");
        if ((rspMap.get(SystemConstant.RSP_CODE)).equals(SystemConstant.RSPCODE_SUCCESS)) {
            return randService.getRand(phone, type, gpsEntity, null, null);
        } else {
            return rspMap;
        }
    }

    /**
     * 查询app初始化信息数据
     *
     * @return
     */

    public Map queryInitParam(HttpServletRequest request, String sign, GpsEntity gpsEntity) {

        String serviceData = gpsEntity.getSerial_number() + gpsEntity.getVersion() + gpsEntity.getAppType() + gpsEntity.getApp_id();
        Map checkSignMap = this.checkCommonSign(request, serviceData, sign, "app初始化");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            List list = headAdvService.getHeadAdvList(gpsEntity, SystemConstant.ADV_PLACE_1);
            Map dataMap = organizationService.getInsAppMap(gpsEntity);
            dataMap.put("cur_date", DateUtil.getCurrDateTimeStr());
            dataMap.put("headAdvList", list);
            dataMap.put("public_key", SystemConstant.PUB_KEY_STR);
            subLog.debug("保存初始化数据logkey");

            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        } else {
            return checkSignMap;
        }
    }

    /**
     * 忘记密码
     *
     * @param request
     * @param login_id
     * @param pass_word
     * @param rand
     * @param gpsEntity
     * @return
     * @throws Exception
     */
    public Map forgetPwd(HttpServletRequest request, String login_id, String pass_word, String rand, GpsEntity gpsEntity) throws Exception {

        String serviceData = gpsEntity.getSerial_number() + StringUtils.objToString(login_id) + StringUtils.objToString(pass_word);
        Map checkSignMap = this.checkCommonSign(request, serviceData, gpsEntity.getSign(), "忘记密码");
        //验签
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
        	//判断验证码是否重复
        	Boolean tempRand = smsCodeFlowService.checkRand(SystemConstant.RAND_SERVICE_TYPE_2, login_id, rand);
        	if (!tempRand) {
        		return this.getRspMap(RspCodeConstant.RSPCODE_200006, null, null);
        	} else {
        		smsCodeFlowService.updRand(SystemConstant.RAND_SERVICE_TYPE_2, login_id, rand);
        	}
            //根据登陆账号获取用户信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomInfByLoginId(login_id);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            if (!SystemConstant.USER_ENABLED.equals(customInf.getUser_st())) {
                super.subLog.info("用户状态异常（用户已停用或已冻结）");
                return super.getRspMap(RspCodeConstant.RSPCODE_200103, null, null);
            }
            //登陆密码加密
            String newPwd = appCustomerInfoService.getNewPassWord(pass_word, login_id, customInf.getCustom_no());
            String forgetPwdRspCode = appCustomerInfoService.forgetPwd(login_id, newPwd, rand, gpsEntity);
            Map dataMap = new HashMap();
            dataMap.put("forgetPwdRspCode", forgetPwdRspCode);
            dataMap.put("cur_date", DateUtil.getCurrDateTimeStr());
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        } else {
        }
        return checkSignMap;
    }

    /**
     * 获取地区
     *
     * @param request
     * @param
     * @param random_number
     * @param
     * @param sign
     * @param login_id
     * @param custom_no
     * @param gpsEntity
     * @return
     */
    public Map getArea(HttpServletRequest request, String sign, String random_number, String login_id, String custom_no, GpsEntity gpsEntity) {

        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(login_id) + StringUtils.objToString(custom_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(), serviceData, login_id, "httpcommon/getArea.do", gpsEntity, "获取地区");
        //验签
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            List dataList = appAreaCdService.getAreaInfo();
            if (CollectionUtil.isEmpty(dataList)) {
                return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, null);
            }
            Map dataMap = new HashMap<>();
            dataMap.put("area_list", dataList);
            subLog.debug("获取我的地区信息:{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 获取总行行号
     *
     * @param request
     * @param random_number
     * @param sign
     * @param login_id
     * @param custom_no
     * @param gpsEntity
     * @return
     */
    public Map getSuperBank(HttpServletRequest request, String sign, String random_number, String login_id, String custom_no, GpsEntity gpsEntity) {

        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(login_id) + StringUtils.objToString(custom_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(), serviceData, login_id, "httpcommon/getSuperBank.do", gpsEntity, "获取总行信息");
        //验签
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            List dataList = superBankService.getSuperBankInfo();
            if (CollectionUtil.isEmpty(dataList)) {
                return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, null);
            }
            Map dataMap = new HashMap<>();
            dataMap.put("super_bank_list", dataList);
            subLog.debug("获取我的银行卡总行信息:{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }

        return checkSignMap;
    }

    /**
     * 获取联行号
     *
     * @param request
     * @param sign
     * @param random_number
     * @param login_id
     * @param custom_no
     * @param area_cd
     * @param super_bank_no
     * @param gpsEntity
     * @return
     */
    public Map getCnaps(HttpServletRequest request, String sign, String random_number, String login_id, String custom_no, String area_cd, String super_bank_no, GpsEntity gpsEntity) {

        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(login_id) + StringUtils.objToString(custom_no) + StringUtils.objToString(area_cd) + StringUtils.objToString(super_bank_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(), serviceData, login_id, "httpcommon/getCnaps.do", gpsEntity, "获取联行号");

        //验签
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            List dataList = infCnapsService.getCnapsInfoByCondition(area_cd, super_bank_no);
            if (CollectionUtil.isEmpty(dataList)) {
                return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, null);
            }
            Map dataMap = new HashMap<>();
            dataMap.put("cnaps_list", dataList);
            subLog.debug("获取我的银行卡联行号信息:{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }

        return checkSignMap;
    }

    /**
     * 查询通知公告列表/消息推送列表
     *
     * @param request
     * @param random_number
     * @param login_id
     * @param custom_no
     * @param send_type     消息类型 1-通知公告 2-消息推送
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getNoticeList(HttpServletRequest request, String random_number,
                             String login_id, String custom_no, String send_type, String begin_tm, String end_tm,
                             String curr_page, String rows_perpage, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no) +
                StringUtils.objToString(curr_page) + StringUtils.objToString(rows_perpage);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getNoticeList.do", gpsEntity, "查询通知公告列表");
        subLog.info("查询通知公告列表调用");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            Map dataMap = new HashMap();
            if (StringUtils.isBlank(curr_page) || StringUtils.isBlank(rows_perpage)) {
                subLog.info("参数为空");
                return super.getRspMap(RspCodeConstant.RSPCODE_200003, null, null);
            }
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            List<Map<String, String>> noticeList = messageInfService.getNoticeList(customInf.getIns_uuid(), customInf.getCustom_uuid(),
                    send_type, begin_tm, end_tm, curr_page, rows_perpage, customInf.getRec_crt_ts());
            dataMap.put("notice_list", noticeList);
            subLog.info("查询通知公告列表完成，接口已返回");
            return super.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 查询通知公告详情
     *
     * @param request
     * @param random_number
     * @param login_id
     * @param custom_no
     * @param message_uuid
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getNoticeDetail(HttpServletRequest request, String random_number,
                               String login_id, String custom_no, String message_uuid, String is_index, String read_st, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no) + StringUtils.objToString(message_uuid);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getNoticeDetail.do", gpsEntity, "查询通知公告详情");
        subLog.info("查询通知公告详情调用");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            Map dataMap = new HashMap();
            if (StringUtils.isBlank(message_uuid)) {
                subLog.info("参数为空");
                return super.getRspMap(RspCodeConstant.RSPCODE_200003, null, null);
            }
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            //通过uuid加载实体
            App_Message_InfEntity entity = messageInfService.getNoticeEntityByUUID(message_uuid);
            if (entity == null) {
                subLog.info("查询参数有误或数据库记录发生变化，未查到对应消息体");
                return super.getRspMap(RspCodeConstant.RSPCODE_200001, null, null);
            }
            dataMap.put("message_title", entity.getMessage_title());
            dataMap.put("message_content", entity.getMessage_content());
            dataMap.put("rec_crt_ts", DateUtil.dateToStr(entity.getRec_crt_ts(), DateUtil.FORMAT_TIME));
            //未读的消息 增加已读记录
            if (SystemConstant.READ_ST_0.equals(read_st)) {
                if (SystemConstant.IS_INDEX.equals(is_index)) {
                    //首页操作 特殊处理 ： 更新所有未读的通知公告消息为已读
                    //逻辑已废弃
//					List<String> uuids = messageInfService.getUnReadNoticeListByCustomUUID(customInf.getIns_uuid(),customInf.getCustom_uuid());
//					if(uuids != null && uuids.size() > 0){
//						customMsgService.saveBatchMsgStatus(customInf.getCustom_uuid(),uuids);
////						for (String uuid : uuids){
////							customMsgService.saveMsgStatus(customInf.getCustom_uuid(),uuid);
////						}
//					}
                } else {
                    //非首页直接把该消息置为已读
                    //新增消息已读记录
                    customMsgService.saveMsgStatus(customInf.getCustom_uuid(), message_uuid);
                }
            }
            subLog.info("查询通知公告详情完成，接口已返回");
            return super.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 获取下一条通知公告
     *
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getNextNoticeInfo(HttpServletRequest request, String login_id, String random_number, String custom_no, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getNextNoticeInfo.do", gpsEntity, "获取下一条通知公告");
        subLog.info("获取下一条通知公告接口调用");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            //查询检查更新
            Map dataMap = new HashMap();
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            //noticeSize 未读通知公告总数
            String noticeSize = messageInfService.getUnReadNoticeCount(customInf.getIns_uuid(), customInf.getCustom_uuid(), customInf.getRec_crt_ts());
            //notice_list List<Map> 通知公告集合
            List<Map<String, Object>> notice_list = messageInfService.getNoticeList(customInf.getIns_uuid(), customInf.getCustom_uuid(), customInf.getRec_crt_ts());
            if (notice_list != null && notice_list.size() > 0) {
                dataMap = notice_list.get(0);
            }
            dataMap.put("noticeSize", noticeSize);
            subLog.info("获取下一条通知公告完成，接口已返回");
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 通知公告全部设置已读
     *
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param message_type
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map setNoticeInfoRead(HttpServletRequest request, String login_id, String random_number, String custom_no, String message_type, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/setNoticeInfoRead.do", gpsEntity, "通知公告全部设置已读");
        subLog.info("通知公告全部设置已读接口调用");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            Map dataMap = new HashMap();
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            List<String> uuids = messageInfService.getUnReadNoticeListByCustomUUID(customInf.getIns_uuid(), customInf.getCustom_uuid(), message_type, customInf.getRec_crt_ts());
            if (uuids != null && uuids.size() > 0) {
                customMsgService.saveBatchMsgStatus(customInf.getCustom_uuid(), uuids);
            }
            subLog.info("通知公告全部设置已读完成，接口已返回");
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 检查更新
     *
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map checkVersion(HttpServletRequest request, String login_id, String random_number, String custom_no, String sign, GpsEntity gpsEntity) {

        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/checkVersion.do", gpsEntity, "检测更新接口");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            //查询检查更新checkVersion
            Map dataMap = organizationService.getInsAppMap(gpsEntity);//organizationService.checkVersion(gpsEntity);
            subLog.info("检测更新{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 客户端帮助信息列表
     *
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getHelpList(HttpServletRequest request, String login_id, String random_number, String custom_no, String sign, GpsEntity gpsEntity) {

        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getHelpList.do", gpsEntity, "客户端帮助信息列表接口");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {

            //客户端帮助信息
            List<Map<String, Object>> help_list = helpService.getHelpList(login_id);
            Map dataMap = new HashMap();
            dataMap.put("help_list", help_list);
            subLog.info("客户端帮助信息列表{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 客户端帮助详细信息
     *
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param help_uuid
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getHelpDetail(HttpServletRequest request, String login_id, String random_number, String custom_no, String help_uuid, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no) + StringUtils.objToString(help_uuid);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getHelpDetail.do", gpsEntity, "客户端帮助信息详细信息接口");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {

            //客户端帮助信息
            Map dataMap = helpService.getHelpDetail(gpsEntity, help_uuid);
            subLog.info("客户端帮助详细信息{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 获取某个类型的客户端帮助信息详细信息
     *
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param help_tp
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getHelpDetailByTp(HttpServletRequest request, String login_id, String random_number, String custom_no, String help_tp, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no) + StringUtils.objToString(help_tp);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getHelpDetailByTp.do", gpsEntity, "获取某个类型的客户端帮助信息详细信息接口");
        subLog.info("获取某个类型的客户端帮助信息详细信息接口调用");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            //客户端帮助信息
            Map dataMap = helpService.getHelpDetailByTp(customInf.getIns_uuid(), help_tp);
            subLog.info("客户端帮助详细信息{}", dataMap);
            subLog.info("获取某个类型的客户端帮助信息详细信息接口调用完成，接口已返回");
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;
    }


    /**
     * 查询我的推广二维码
     *
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map getMyShareQr(HttpServletRequest request, String login_id, String random_number, String custom_no, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getMyShareQr.do", gpsEntity, "查询我的推广二维码接口");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            //分享说明
            String share_desc = organizationService.getShareDesc(custom_no);
            //分享标题
            String share_title = organizationService.getShareTitle(custom_no);
            //长链接转短链接
            Map jsonMap = new HashMap();
            //缓存中取得分享链接
            Map regUrlMap = new HashMap();
            List<Map<String, String>> regUrl = systemService.getCodeListByTp(SystemConstant.REGURL_TP);
            if (regUrl != null && regUrl.size() > 0) {
                for (Map<String, String> map : regUrl) {
                    if (SystemConstant.REGURL_VAL.equals(StringUtils.objToString(map.get("code_val")))) {
                        regUrlMap.put("regUrl", StringUtils.objToString(map.get("code_desc")));
                    }
                }
            }
            //未找到 则设置为 ""
            if (regUrlMap.get("regUrl") == null) {
                regUrlMap.put("regUrl", "");
            }

            String url = regUrlMap.get("regUrl") + "/httpcommon/httpReg.do?sup_custom_uuid=" + customInf.getCustom_uuid() + "&time=" + System.currentTimeMillis();
            /*String url = "https://192.168.12.211:8443/mpos/httpcommon/httpReg.do?sup_custom_uuid=1";*/
            try {
		/*		HttpPost httpost = new HttpPost("https://dwz.cn/create.php");
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("url", url));
				httpost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
				CloseableHttpResponse response = httpclient.execute(httpost);
				String jsonStr = EntityUtils.toString((response).getEntity(), "utf-8");
				System.out.println(jsonStr);
				JSONObject object = JSON.parseObject(jsonStr);
				jsonMap.put("share",object.getString("tinyurl"));*/
                subLog.info("长链接{},", url);
                //		subLog.info("短链接{},",String.valueOf(jsonMap.get("share")));
            } catch (Exception e) {
                e.printStackTrace();
            }
            Map dataMap = new HashMap();
            if (StringUtils.isEmpty(jsonMap.get("share"))) {
                dataMap.put("share_url", String.valueOf(jsonMap.get("share")));
            }
            dataMap.put("share_url", url);
            dataMap.put("share_title", share_title);
            dataMap.put("share_desc", share_desc);
            subLog.info("查询我的推广二维码接口{}", dataMap);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;
    }

    /**
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param affix_tp
     * @param store_tp
     * @param imgContent
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map uploadPic(HttpServletRequest request, HttpServletResponse response, String login_id, String random_number, String custom_no, String affix_tp, String store_tp,
                         MultipartFile imgContent, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no)
                + StringUtils.objToString(affix_tp) + StringUtils.objToString(store_tp);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/uploadPic.do", gpsEntity, "单张上传图片");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            if (StringUtils.isBlank(login_id) || StringUtils.isBlank(custom_no) || StringUtils.isBlank(affix_tp) || StringUtils.isBlank(store_tp) || imgContent == null) {
                subLog.info("参数信息不全，返回");
                return super.getRspMap(RspCodeConstant.RSPCODE_200003, null, null);
            }
            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            //设置大小类，上传照片
            MposResponse mposResponse = super.uploadImgForApp(request, response, imgContent, customInf.getCustom_uuid(),
                    affix_tp, store_tp, login_id, custom_no);
            if (ExceptionConstant.ERROR.getCode().equals(mposResponse.getRespCode())) {
                subLog.info("图片上传异常");
                return super.getRspMap(RspCodeConstant.RSPCODE_200108, null, null);
            }
            Map dataMap = new HashMap();
            dataMap.put("affix_uuid", StringUtils.objToString(mposResponse.getRespData()));
            subLog.info("图片上传成功，接口已返回");
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, dataMap);
        }
        return checkSignMap;
    }

    /**
     * 退出登陆
     *
     * @param request
     * @param login_id
     * @param random_number
     * @param custom_no
     * @param sign
     * @param gpsEntity
     * @return
     */
    public Map logout(HttpServletRequest request, String login_id, String random_number, String custom_no, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(), serviceData, login_id, "httpcommon/logout.do", gpsEntity, "退出登录");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {
            Map<String, String> map = new HashMap<>();
            map.put(SystemConstant.SESSION_LOGIN_USER, login_id);
            loginService.logout(random_number, map);
            Map dataMap = new HashMap<>();
            subLog.info("已退出登陆");
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);

        }
        return checkSignMap;
    }


    /**
     * /**
     * 根据分享类型获取分享信息列表
     */
    public Map getShareByTp(HttpServletRequest request, String login_id, String random_number, String custom_no, String share_tp, String curr_page, String rows_perpage, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no)
                + StringUtils.objToString(share_tp);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getShareByTp.do", gpsEntity, "根据分享类型获取分享信息列表");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {

            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }
            if (StringUtils.isEmpty(curr_page)) {
                return super.getRspMap(RspCodeConstant.RSPCODE_200001, null, null);
            }

            if (StringUtils.isEmpty(rows_perpage)) {
                return super.getRspMap(RspCodeConstant.RSPCODE_200001, null, null);
            }
            if (StringUtils.isEmpty(share_tp)) {
                return super.getRspMap(RspCodeConstant.RSPCODE_200116, null, null);
            }
            //长链接转短链接
            Map jsonMap = new HashMap();
            Map regUrlMap = new HashMap();
            List<Map<String, String>> regUrl = systemService.getCodeListByTp(SystemConstant.REGURL_TP);
            if (regUrl != null && regUrl.size() > 0) {
                for (Map<String, String> map : regUrl) {
                    if (SystemConstant.REGURL_VAL.equals(StringUtils.objToString(map.get("code_val")))) {
                        regUrlMap.put("regUrl", StringUtils.objToString(map.get("code_desc")));
                    }
                }
            }
            //未找到 则设置为 ""
            if (regUrlMap.get("regUrl") == null) {
                regUrlMap.put("regUrl", "");
            }

            String url = regUrlMap.get("regUrl") + "/httpcommon/httpReg.do?sup_custom_uuid=" + customInf.getCustom_uuid() + "&time=" + System.currentTimeMillis();
            /*String url = "https://192.168.12.211:8443/mpos/httpcommon/httpReg.do?sup_custom_uuid=1";*/
            try {
		/*		HttpPost httpost = new HttpPost("https://dwz.cn/create.php");
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("url", url));
				httpost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
				CloseableHttpResponse response = httpclient.execute(httpost);
				String jsonStr = EntityUtils.toString((response).getEntity(), "utf-8");
				System.out.println(jsonStr);
				JSONObject object = JSON.parseObject(jsonStr);
				jsonMap.put("share",object.getString("tinyurl"));*/
                subLog.info("长链接{},", url);
                //		subLog.info("短链接{},",String.valueOf(jsonMap.get("share")));
            } catch (Exception e) {
                e.printStackTrace();
            }
            Map dataMap = new HashMap();
            if (StringUtils.isEmpty(jsonMap.get("share"))) {
                dataMap.put("share_url", String.valueOf(jsonMap.get("share")));
            }

            List share_list = shareService.getShareByTp(customInf.getIns_uuid(), share_tp, curr_page, rows_perpage, gpsEntity);
            dataMap.put("share_url", url);
            dataMap.put("share_list", share_list);
            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;

    }

    /**
     * 根据分享内容主键获取分享信息详情
     */
    public Map getShareDetailById(HttpServletRequest request, String login_id, String random_number, String custom_no, String share_uuid, String sign, GpsEntity gpsEntity) {
        String serviceData = StringUtils.objToString(gpsEntity.getSerial_number()) + StringUtils.objToString(custom_no)
                + StringUtils.objToString(share_uuid);
        Map checkSignMap = this.getLogClientKeyEntity(request, gpsEntity.getSerial_number(), sign, random_number, gpsEntity.getVersion(),
                serviceData, login_id, "httpcommon/getShareDetailById.do", gpsEntity, "根据分享内容主键获取分享信息详情");
        if (checkSignMap.get(SystemConstant.RSP_CODE).equals(SystemConstant.RSPCODE_SUCCESS)) {

            //根据登录账户得到登录会员信息
            App_Custom_InfEntity customInf = appCustomerInfoService.getCustomByNo(custom_no);
            //未查到对应用户信息
            if (customInf == null || StringUtils.isBlank(customInf.getCustom_uuid())) {
                subLog.info("登录账户无效");
                return super.getRspMap(RspCodeConstant.RSPCODE_200007, null, null);
            }

            //分享信息主键
            if (StringUtils.isEmpty(share_uuid)) {
                return super.getRspMap(RspCodeConstant.RSPCODE_200117, null, null);
            }

            Map dataMap = shareService.getShareDetailById(customInf.getIns_uuid(), share_uuid, gpsEntity);
            if (CollectionUtil.isEmpty(dataMap) && dataMap == null) {
                return super.getRspMap(RspCodeConstant.RSPCODE_200118, null, null);
            }

            return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, SystemConstant.RSPCODE_SUCCESS_MSG, dataMap);
        }
        return checkSignMap;

    }

    /**
     * 错误文件上传
     *
     * @param request
     * @return
     */
    public Map uploadErrorReport(HttpServletRequest request, HttpServletResponse response,
                                 GpsEntity gpsEntity,
                                 MultipartFile err_report_file,String login_id) {

        //设置大小类，上传文件
        MposResponse mposResponse = this.uploadFile(request, response, gpsEntity, err_report_file,login_id);
        if (ExceptionConstant.ERROR.getCode().equals(mposResponse.getRespCode())) {
            subLog.info("文件上传异常");
            return super.getRspMap(RspCodeConstant.RSPCODE_200108, null, null);
        }
        Map dataMap = new HashMap();
        subLog.info("文件上传成功");
        return this.getRspMap(SystemConstant.RSPCODE_SUCCESS, null, dataMap);


    }

    /**
     * 文件上传
     *
     * @param request
     * @return
     */
    @ResponseBody
    public MposResponse uploadFile(HttpServletRequest request, HttpServletResponse response,
                                   GpsEntity gpsEntity,
                                   MultipartFile err_report_file,String login_id) {
        MposResponse resp = new MposResponse();
        App_Log_ErrorReportEntity appLogErrorReportEntity = new App_Log_ErrorReportEntity();
        try {
            /*重命名 保存文件*/
            String uploadName = IDFactoryUtil.getId() + ".cr";
            /*根据保存的路径*/
            StringBuffer filePath = new StringBuffer("/")
                    .append(SystemConstant.FILE_UPLOAD_PATH).append("/")
                    .append("errorReport").append("/");

            String uploadPath = request.getServletContext().getRealPath("/") + filePath;
            subLog.info("uploadPath:"+uploadPath);
            /*保存文件*/
            File _file = FileUtils.uploadFile(err_report_file, uploadPath,uploadName);
            if (_file != null) {
                appLogErrorReportEntity.setReport_uuid(IDFactoryUtil.getUUID());

                appLogErrorReportEntity.setFile_path(filePath+uploadName);
                appLogErrorReportEntity.setApp_type(gpsEntity.getAppType());
                appLogErrorReportEntity.setLogin_id(login_id);
                appLogErrorReportEntity.setApp_id(gpsEntity.getApp_id());
                appLogErrorReportEntity.setSystemname(gpsEntity.getSystem_name());
                appLogErrorReportEntity.setSystemuuid(gpsEntity.getSystem_uuid());

                appLogErrorReportEntity.setSystemversion(gpsEntity.getSystem_version());
                appLogErrorReportEntity.setSystemdevicemodel(gpsEntity.getSystem_device_model());
                appLogErrorReportEntity.setClient_version(gpsEntity.getVersion());
                appLogErrorReportEntity.setReport_st(SystemConstant.REPORT_ST);

                appLogErrorReportEntity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
                appLogErrorReportEntity.setLast_oper_in(SystemConstant.OPER_ADD);
                appLogErrorReportEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
                appLogErrorReportEntity.setRec_st(SystemConstant.NORMAL_STATUS);
                appLogErrorReportEntity.setRec_upd_usr_id("admin");
                appLogErrorReportDao.save(appLogErrorReportEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resp = new MposResponse(ExceptionConstant.ERROR);
        }
        return resp;
    }

    /**
     * 获取上传路径
     *
     * @param uploadType
     * @return
     */
    private String getErrorFilePath(String uploadType) {
        StringBuffer path = new StringBuffer("/");
        path.append(SystemConstant.FILE_UPLOAD_PATH);
        if (!StringUtils.isBlank(uploadType)) {
            path.append("/")
                    .append(uploadType);
        }
        path.append("/")
                .append(DateUtil.getCurrDateStr());
        return path.toString();
    }


}
